﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Microsoft.Practices.Unity;
using Vit.Common;
using Vit.Common.Extensions;
using Vit.Web.Providers;
using Vit.Web.Models;
using Vit.Web.Mvc.Results;
using Vit.Web.Helpers;
using Vit.Web.Modules;

namespace Vit.Web.Mvc.Filters
{
    public class PageViewFilter : IActionFilter, IMvcFilter
    {
        private const string DesignPermissionUrl = "/Page/Design";
        private const string ManagePermissionUrl = "/Page/Manage";

        public PageViewFilter()
        {
        }

        public void OnActionExecuted(ActionExecutedContext filterContext)
        {
        }

        public void OnActionExecuting(ActionExecutingContext filterContext)
        {
            string areaName = (filterContext.RouteData.DataTokens["area"] ?? "").ToString();
            if(!string.IsNullOrEmpty(areaName))
            {
                AreaModuleBase areaModule = Unity.Current.Resolve<IModule>(areaName.ToLower()) as AreaModuleBase;
                filterContext.HttpContext.Items.Add("areaModule", areaModule);
            }

            PageMode pageMode = PageMode.Visit;
            filterContext.Controller.ViewBag.CurrentPageMode = PageMode.Visit;
            if (filterContext.RouteData.Values.ContainsKey("pageMode"))
            {
                filterContext.Controller.ViewBag.CurrentPageMode = (PageMode)filterContext.RouteData.Values["pageMode"];
            }

            var providers = Unity.Current.ResolveAny<IMembershipProvider>();
            if (providers.ForEach<IMembershipProvider, bool>(o => o.CanUserAccess(PortalContext.Current.User.Account, DesignPermissionUrl)))
            {
                pageMode = pageMode | PageMode.Design;
            }
            if (providers.ForEach<IMembershipProvider, bool>(o => o.CanUserAccess(PortalContext.Current.User.Account, ManagePermissionUrl)))
            {
                pageMode = pageMode | PageMode.Manage;
            }
            filterContext.Controller.ViewBag.PageMode = pageMode;


            if (PortalContext.Current.Page != null)
            {
                var page = PortalContext.Current.Page;

                filterContext.Controller.ViewBag.Page = page;
                filterContext.Controller.ViewBag.CharSet = page.CharSet;
                filterContext.Controller.ViewBag.Title = page.Title;
                filterContext.Controller.ViewBag.MetaKeywords = page.MetaKeywords;
                filterContext.Controller.ViewBag.MetaDescription = page.MetaDescription;
                filterContext.Controller.ViewBag.CultureInfo = page.CultureInfo;
                filterContext.Controller.ViewBag.FavIcon = page.WebSite.FavIcon;
            }
        }

        #region IMvcFilter Members

        public bool AllowMultiple
        {
            get { return false; }
        }

        public int Order
        {
            get { return (int)FilterScope.Global; }
        }

        #endregion
    }
}
